package cn.com.xinjinfu.pdoulcProduct.engine;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

import com.android.volley.Response.Listener;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.login.LoginActivity;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;

/**
 * 请求正确回调
 *
 * @author shi
 */
public class JsonListener implements Listener<JSONObject> {
    private Dialog dialog;
    private Handler hand;
    private Context ct;
    private boolean handError;
    private static Dialog loginDialog = null;

    public JsonListener(Context context, Handler handler, Dialog dg, boolean handError) {
        hand = handler;
        dialog = dg;
        ct = context;
        this.handError = handError;
    }

    @Override
    public void onResponse(JSONObject response) {
        L.d(response.toString());
        if (dialog != null)
            dialog.dismiss();
        try {
            int x = response.getInt("code");
            switch (x) {
                case -1:
                    if (hand != null) {
                        Message m = new Message();
                        m.what = ComAsk.Success;
                        m.obj = response;
                        hand.sendMessage(m);
                    }
                    return;
                case -2:
                    if (handError && hand != null)
                        hand.sendEmptyMessage(x);
                    if (loginDialog == null || !loginDialog.isShowing())
                        showloginDialog();
                    break;
                default:
                    if (handError && hand != null)
                        hand.sendEmptyMessage(x);
                    ToastManager.showShort(ct, response.getString("msg"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示登录过期dialog
     */
    private void showloginDialog() {
        // 构造对话框
        loginDialog = UIManager.getCommWarnDialog(ct,
                R.layout.layout_common_dialog, R.string.login_overdue,
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.warn_sure_bt:
                                loginDialog.dismiss();
                                Intent it = new Intent(ct, LoginActivity.class);
                                ct.startActivity(it);
                                ((Activity) ct).finish();
                                break;
                            case R.id.warn_cancle_bt:
                                loginDialog.dismiss();
                                ((Activity) ct).finish();
                                break;
                            default:
                                break;
                        }
                    }
                });
        loginDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                ((Activity) ct).finish();
            }
        });
        loginDialog.setCancelable(true);
        loginDialog.setCanceledOnTouchOutside(false);
        loginDialog.show();
    }

}
